﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using System.Data;
using DevExpress.Web.Mvc;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 银行收入管理
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class FwmmyhsrController : AdminBaseController
    {
        //
        // GET: /Admin/Fwmmyhsr/
        [AuthorityActionFilter("m_Fwmmyhsr", Order = 1)]
        [LogPageActionFilter("银行收入管理", Order = 2)]
        /// <summary>
        /// 银行收入管理
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 数据列表
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult FwmmyhsrList() {

            int pageIndex = 1;
            List<Model.Fwmmyhsr> fwmmyhsrList = GetFwmmyhsrList(pageIndex);

            return View(fwmmyhsrList);

        }

        /// <summary>
        /// 用于页面的刷新
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult FwmmyhsrListPartial() {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string yhNo = Request.Params["yhNo"];
            string callbackArg = DevExpressHelper.CallbackArgument;
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            List<Model.Fwmmyhsr> fwmmyhsrList = GetFwmmyhsrList(pageIndex, pageCount, pageRowSize);


            return PartialView("FwmmyhsrListPartial", fwmmyhsrList);
        
        }



        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageCount">每页显示大小</param>
        /// <param name="pageRowSize">每页显示页数</param>
        /// <returns>数据集合</returns>
        private List<Model.Fwmmyhsr> GetFwmmyhsrList(int pageIndex, int pageCount = 0, int pageRowSize = 0)
        {
            BLL.Fwmmyhsr fwmmyhsrBll = new BLL.Fwmmyhsr();

            BLL.Common commonBll = new BLL.Common();
            string fiedls = "*";
            int total;
            DataSet ds = commonBll.GetListByPage("[nc].nc_fwmmyhsr", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, "", "srsj", 1, out total);

            List<Model.Fwmmyhsr> fwmmyhsrAllList = new List<Model.Fwmmyhsr>();
            List<Model.Fwmmyhsr> fwmmyhsrList = fwmmyhsrBll.DataTableToListFwmmyhsr(ds.Tables[0]);

            pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                fwmmyhsrAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (fwmmyhsrList.Count > i)
                {
                    fwmmyhsrAllList[((pageIndex - 1) * pageRowSize) + i] = fwmmyhsrList[i];
                }
            }
            return fwmmyhsrAllList;
        }

        /// <summary>
        /// 初始化页面
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>视图</returns>
        public ActionResult FwmmyhsrContent(string id) {
            Model.Fwmmyhsr fwmmyhsrModel = new Model.Fwmmyhsr();
            BLL.Fwmmyhsr fwmmyhsrBll = new BLL.Fwmmyhsr();
            if (string.IsNullOrEmpty(id))//增加
            {
                fwmmyhsrModel.khyh = "鄞州银行";
                fwmmyhsrModel.srsj = DateTime.Now;

            }
            else {//修改
                id = Fetch.estimateString(id, CheckGetEnum.Safety);
                fwmmyhsrModel = fwmmyhsrBll.GetModel(decimal.Parse(id));
                ViewData["id"] = id;
            
            }
            return View(fwmmyhsrModel);
        
        }

       /// <summary>
       /// 保存
       /// </summary>
       /// <param name="fwmmyhsrModel">model对象</param>
       /// <returns>视图</returns>
        public ActionResult SaveFwmmyhsr(Model.Fwmmyhsr fwmmyhsrModel) {

            if (Fetch.Post("hidid") == "" || Fetch.Post("hidid") == null)
            {
                fwmmyhsrModel.id = fwmmyhsrModel.id;
            }
            else
            {
                fwmmyhsrModel.id = decimal.Parse(Fetch.Post("hidid"));
            }
            fwmmyhsrModel.khyh = DropDownEditExtension.GetValue<string>("tbkhyh");
            fwmmyhsrModel.srbz = Fetch.Post("tbsrbz");
            fwmmyhsrModel.srje = decimal.Parse(Fetch.Post("tbsrje"));
            fwmmyhsrModel.srsj = DateTime.Parse(Fetch.Post("tbsrsj"));

            string type = "add";
            string srje = Fetch.Post("hidsrje");
            if (srje != string.Empty)
            {
                type = "edit";
            }

            ActionResult actionResult = null;
            BLL.Fwmmyhsr fwmmyhsrBll = new BLL.Fwmmyhsr();
            try
            {
                if (type == "add")
                {
                    fwmmyhsrBll.Add(fwmmyhsrModel);
                }
                else if (type == "edit")
                {
                    fwmmyhsrBll.Update(fwmmyhsrModel);
                
                }

                ViewData["message"] = "操作成功";
                ViewData["IsClose"] = "Y";
                actionResult = View("FwmmyhsrContent", fwmmyhsrModel);

            }
            catch (Exception)
            {

                ViewData["message"] = "保存失败";
                ViewData["IsClose"] = "N";
                actionResult = View("FwmmyhsrContent", fwmmyhsrModel);

            }
            return actionResult;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>字符串</returns>
        public string Delete(string id) {
            string str = "";
            id = Fetch.estimateString(id, CheckGetEnum.Safety);
            BLL.Fwmmyhsr fwmmyhsrBll = new BLL.Fwmmyhsr();
            try
            {
                fwmmyhsrBll.Delete(decimal.Parse(id));
                str = "ok";
            }
            catch (Exception)
            {
                str = "error";
            }

            return str;
        
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids">主键</param>
        /// <returns>是否删除</returns>
        public string DeleteMany(string ids) {
            string str = "";

            string[] idArray = ids.Split(new string[] { "%!^" }, StringSplitOptions.RemoveEmptyEntries);
            BLL.Fwmmyhsr fwmmyhsrBll = new BLL.Fwmmyhsr();

            try
            {
                fwmmyhsrBll.DeleteMany(idArray);
                str = "ok";
            }
            catch (Exception)
            {
                str = "error";
            }

            return str;
        
        }


    }
}
